﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlowPipe
{
    public partial class FlowPipe : UserControl
    {
        public FlowPipe()
        {
            InitializeComponent();
            //减少闪烁
            this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
            this.SetStyle(ControlStyles.ResizeRedraw,true);
            this.SetStyle(ControlStyles.Selectable,true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
            this.SetStyle(ControlStyles.UserPaint, true);
            //定时器
            Timer myTimer = new Timer();
            //500毫秒刷新一次
            myTimer.Interval = 3000;
            //触发事件
            myTimer.Tick += myTimer_Tick;
            //启动
            myTimer.Enabled = true;
        }

        private void myTimer_Tick(object sender, EventArgs e)
        {
            //绘制图画
             this.Invalidate();
        }

        //画布
        Graphics g;
        float width;
        float heigth;
        public bool IsActive { get; set; }
        //流动颜色
        Color runColor = Color.FromArgb(128, 128, 255);
        public Color RunColor
        {
            get
            {
                return runColor;
            }
            set
            {
                runColor = value;
            }
        }
        //开始颜色
        Color startColor = Color.FromArgb(73, 73, 73);
        public Color StartColor
        {
            get { return startColor; }
            set { startColor = value; }
        }
        //结束颜色
        //开始颜色
        Color endColor = Color.FromArgb(247, 247, 247);
        public Color EndColor
        {
            get { return endColor; }
            set { endColor = value; }
        }
        //定义流动条长度
        int length = 20;
        public int Length { get { return length; } set { length = value; } }
        // 定义流动条宽度占比
        private double extent = 0.75;
        public double Extent
        {
            get { return extent; }
            set { extent = value; }
        }
        //定义流动条间距
        private int space = 5;
        public int Space
        {
            get { return space; }
            set { space = value; }
        }
        //流动条速度
        private int speed = 5;
        public int Speed
        {
            get { return speed; }
            set
            {
                speed = value;
            }
        }
        //方向  true 是垂直
        private bool isVertical = true;
        public bool IsVertical
        {
            get { return isVertical; }
            set
            {
                isVertical = value;
            }
        }
        //流动状态,1 从左到右，2 从右到左
        // 3 从上倒下，4 从先道上
        private int flowNum = 1;
        public int FlowNum
        {
            get { return flowNum; }
            set { flowNum = value; }
        }
        
        void ColorChange(int i)
        {
            i = i % 7;
            switch (i)
            {
                case 1:
                    RunColor = Color.FromArgb(234, 13, 46);
                    break;
                case 2:
                    RunColor = Color.FromArgb(242, 113, 21);
                    break;
                case 3:
                    RunColor = Color.FromArgb(234, 229, 8);
                    break;
                case 4:
                    RunColor = Color.FromArgb(33, 232, 21);
                    break;
                case 5:
                    RunColor = Color.FromArgb(10, 224, 178);
                    break;
                case 6:
                    RunColor = Color.FromArgb(10, 155, 237);
                    break;
                default:
                    RunColor = Color.FromArgb(126, 5, 211);
                    break;
            }
    
        }
        async Task ACKRenderingS(Graphics gs,int i)
        {
            await Task.Run(async () =>
            {
                await Task.Delay(50);
                ColorChange(i);
                //定义单色画笔
                SolidBrush sb = new SolidBrush(RunColor);

                // 笔刷，起始位置x，y,图形长度x，y
                // g.FillRectangle(sb,(Length + speed)*i,(float)(1 - Extent) / 2 * heigth, Length,(float)(heigth * extent * 0.1));
                var t = new RectangleF((Length + speed) * i, (float)(1 - Extent) / 2 * heigth, Length, (float)(heigth * extent * 1.0));
                try
                {
                    gs.FillRectangle(sb, t);
                }
                catch (Exception es)
                {
                    MessageBox.Show(es.Message);
                }
            });
        }
        //控制条形住的方向，odd为奇数从左到右，偶数从右到左
        int odd = 1;
      async  Task Rendering()
        {
            using (Graphics gs = this.CreateGraphics())
            {
                if (isVertical) {
               if(odd % 2 != 0 )
                {
                    //绘制滚动条
                    for (int i = 1; i < width / (Length + speed) - 1; i++)
                    {

                      await  ACKRenderingS(gs, i);

                    }

                }
                else
                {
                    //绘制滚动条
                    for (int i = (int)(width / (Length + speed) - 1); i > 1; i--)
                    {

                      await  ACKRenderingS(gs, i);

                    }

                    }
                }
                else
                {
                    //绘制滚动条
                    for (int i = 1; i < heigth / (Length + speed) - 1; i++)
                    {

                        await Task.Run(async () =>
                        {
                            await Task.Delay(50);
                            ColorChange(i);
                            //定义单色画笔
                            SolidBrush sb = new SolidBrush(RunColor);

                            // 笔刷，起始位置x，y,图形长度x，y
                            // g.FillRectangle(sb,(Length + speed)*i,(float)(1 - Extent) / 2 * heigth, Length,(float)(heigth * extent * 0.1));
                            var t1 = new RectangleF( (float)(1 - Extent) / 2 * width, (Length + speed) * i,  (float)(width * extent * 1.0),Length);
                           // var t2 = new RectangleF((float)((1 - Extent) / 2 * width + width / 2.0), (Length + speed) * i, (float)(heigth * extent * 1.0), Length);
                            try
                            {
                                gs.FillRectangle(sb, t1);
                               // gs.FillRectangle(sb, t2);
                            }
                            catch (Exception es)
                            {
                                MessageBox.Show(es.Message);
                            }
                        });

                    }
                }

            }
            odd++;
        }

        //重绘
        protected override async void OnPaint(PaintEventArgs e)
        {
           
            base.OnPaint(e);
            //获取画布对象
            g = e.Graphics;
            //画质质量，抗锯齿 ,平滑
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //高质量
            g.SmoothingMode = SmoothingMode.HighQuality;
            width = this.Width; // 画布宽
            heigth = this.Height; //画布高
            //绘制第一步，绘制渐变色外框
            //拿到刷子
            LinearGradientBrush brush;
            RectangleF rec1;
            //矩形
            if (isVertical)
            {
                rec1 = new RectangleF(0, 0, width, heigth / 2);
            }
             else
            {
                 rec1 = new RectangleF(0, 0, width / 2, heigth );
            }
            //位置，大小，起始渐变色，结束渐变色，从上往下
            brush = new LinearGradientBrush(rec1, startColor, endColor, isVertical ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal);
            g.FillRectangle(brush, rec1);

            //如果是偶数就将第二个方形下降，否则不下降。
            float h1, h2;
            RectangleF rec2;
             if (isVertical)
            {
               h1 = heigth % 2 == 0 ? (float)(heigth / 2.0 - 0.5) : (float)(heigth / 2.0);
               h2 = heigth % 2 == 0 ? (float)(heigth / 2.0 + 0.5) : (float)(heigth / 2.0);
                //不能让两个矩形的坐标有重叠，
                rec2 = new RectangleF(0, h1, width, h2);
            }
             else
            {
                h1 = width % 2 == 0 ? (float)(width / 2.0 - 0.5) : (float)(width / 2.0);
                h2 = width % 2 == 0 ? (float)(width / 2.0 + 0.5) : (float)(width / 2.0);
                //不能让两个矩形的坐标有重叠，
                 rec2 = new RectangleF(h1, 0, h2, heigth);
            }


            //位置，大小，起始渐变色，结束渐变色，从上往下
            brush = new LinearGradientBrush(rec2, endColor,
                startColor, isVertical ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal
                );
            g.FillRectangle(brush, rec2);
            // 绘制流动条
            //SolidBrush sb;
            switch (FlowNum)
            {
                case 1:

                await    Rendering();
                    break;
            }



        }


    }
}
